package com.hotacorp.opencloud.common.resourcecommon.exception;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hotacorp.opencloud.common.commdata.Result;

/**
 * 无权限返回处理类
 * @author lwg
 *
 */
public class AuthAccessDeniedHandler implements AccessDeniedHandler {

	@Override
	public void handle(HttpServletRequest request, HttpServletResponse response,
			AccessDeniedException accessDeniedException) throws IOException, ServletException {
        Result<String> ret = new Result<String>("", HttpStatus.FORBIDDEN.value(), accessDeniedException.getLocalizedMessage());
        response.setContentType("application/json");
        response.setStatus(HttpStatus.OK.value());
        try {
            ObjectMapper mapper = new ObjectMapper();
            mapper.writeValue(response.getOutputStream(), ret);
        } catch (Exception e) {
            throw new ServletException();
        }
	}
}
